var nowDate = getDay(0);
var calendar = new lCalendar();
var starttimecalendar = new lCalendar();
var endtimecalendar = new lCalendar();
var pandiancalendar = new lCalendar();
var url;
var shopJson={};
starttimecalendar.init({
	'trigger': '#startDate',
	'type': 'date',
	'minDate': '2021-01-01',
	'maxDate': '2099-12-31'
});
endtimecalendar.init({
	'trigger': '#endDate',
	'type': 'date',
	'minDate': '2021-01-01',
	'maxDate': '2099-12-31'
});
pandiancalendar.init({
	'trigger': '#pandianDate',
	'type': 'date',
	'minDate': '2022-01-01',
	'maxDate': '2099-12-31'
});
function ICalendarClick(){
//	onload()
}
$(document).ready(function(){
	$('#name').text(sessionStorage.getItem("name"));
	$('#startDate').val(nowDate);
	$('#endDate').val(nowDate);
	$('#pandianDate').val(nowDate);
	$('#searchButton').on('click',function(){
		if($('#startDate').val() > $('#endDate').val()){
			layer.alert("开始日期不能大于结束日期")
		}else{
	/*		console.log($('#startDate').val());
			console.log($('#endDate').val());
			console.log($('#shopcode').val());
			console.log($('#flagSelect').val());*/
			getData();
		}
	});
	$('#addButton').on('click',function(){
		getShopInfo()
	});
	$('#saveButton').on('click',function(){
		savePandian();
	});
	getData();
	getPandianShop();
});
//保存盘点疾患
function savePandian(){
	
	var shopList = [];
	for(var el in shopJson){
		var shopinfo = shopJson[el];
		//新添加的
		if(shopinfo.id == undefined){
			shopinfo.flag = 0;
			shopinfo.pandianDate = $('#pandianDate').val();
			shopList.push(shopinfo);
		}
	}
	if(shopList.length > 0){
		showloading();
		var sendData = {};
		sendData.shopList=shopList;
		sendData.userId=sessionStorage.getItem("userId");
		$.ajax({
			url: "insertPandianPlan"
			,type:'post'
//			,async: false
			,data:JSON.stringify(sendData)
			,contentType: "application/json; charset=utf-8"
			,dataType:'json'
			,success: function(res){
				if(res.success){
					location.reload();
				}else{
					hideloading();
					layer.alert(res.message);
				}
			}
			,error:function(XMLHttpRequest, textStatus, errorThrown) {
				 console.log(XMLHttpRequest);
				 console.log(textStatus);
				 console.log(errorThrown);
				 hideloading();
			}
		});
	}else{
		layert.alert('没有新增加的盘点计划');
	}
}
//删除盘点疾患
function deletePlan(id){
	console.log(typeof id)
	if(typeof id == 'object'){
		$(id).parent().remove();
		delete shopJson[$(id).attr('shopcode')];
		console.log(shopJson)
	}else{
		var sendData = {};
		sendData.id=id;
		$.ajax({
			url: "deletePandianPlan"
				,type:'post'
//		,async: false
					,data:JSON.stringify(sendData)
					,contentType: "application/json; charset=utf-8"
						,dataType:'json'
							,success: function(res){
								if(res.success){
									location.reload();
								}else{
									layer.alert(res.message);
								}
							}
		,error:function(XMLHttpRequest, textStatus, errorThrown) {
			console.log(XMLHttpRequest);
			console.log(textStatus);
			console.log(errorThrown);
			hideloading();
		}
		});
	}
}
//查询门店信息
function getShopInfo(){
	if($('#pandianShop').val() == ''){
		layer.alert('门店号不能为空')
	}else{
		showloading();
		var sendData = {};
		sendData.shopcode=$('#pandianShop').val();
		$.ajax({
			url: "getShopInfo"
			,type:'post'
//			,async: false
			,data:JSON.stringify(sendData)
			,contentType: "application/json; charset=utf-8"
			,dataType:'json'
			,success: function(res){
				console.log(res)
				try {
					if(res.success){
						var resdata = res.data[0];
						var shopInfo = {};
						shopInfo.shopcode = resdata.shopcode;
						shopInfo.shopname = resdata.shopname;
						if(shopJson[resdata.shopcode] == undefined){
							shopJson[resdata.shopcode] = shopInfo;
							$('#opeTable').append('<tr class="thin-border-bottom">'
									+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+resdata.shopcode+'</span></td>'
									+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+resdata.shopname+'</td>'//id="'+el+obj+i+'"
									+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;" class="orange" shopcode="'+resdata.shopcode+'" onclick="deletePlan(this)">删除</td>'
									+'</tr>');
						}else{
							layer.alert('此门店已经添加');
						}
						
					}else{
						layer.alert(res.message);
					}
				} catch (e) {
					console.log(e)
				} finally{
					hideloading();
				}
				
			}
			,error:function(XMLHttpRequest, textStatus, errorThrown) {
				 console.log(XMLHttpRequest);
				 console.log(textStatus);
				 console.log(errorThrown);
				 hideloading();
			}
		});
	}
	
}
//查看已经添加的门店
function getPandianShop(){
	var sendData = {};
	sendData.startDate = $('#pandianDate').val();
	sendData.endDate = $('#pandianDate').val()
	sendData.shopcode=$('#shopcode').val();
   	sendData.flag=0;
	$.ajax({
		url: "getPandianPlan"
		,type:'post'
//		,async: false
		,data:JSON.stringify(sendData)
		,contentType: "application/json; charset=utf-8"
		,dataType:'json'
		,success: function(res){
			console.log(res.data);
			$('#opeTable').empty();
			if(res.success){
				var resdata = res.data;
				var trStr='';
				for (var i = 0; i < resdata.length; i++) {
					
					$('#opeTable').append('<tr class="thin-border-bottom">'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+resdata[i].shopcode+'</span></td>'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+resdata[i].shopname+'</td>'//id="'+el+obj+i+'"
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;" class="orange" onclick="deletePlan('+resdata[i].id+')">删除</td>'
							+'</tr>');
					var shopInfo = {};
					shopInfo.id = resdata[i].id;
					shopInfo.shopcode = resdata[i].shopcode;
					shopInfo.shopname = resdata[i].shopname;
					shopJson[resdata[i].shopcode] = shopInfo;
				}
			}else{
				layer.alert(res.message);
			}
		}
		,error:function(XMLHttpRequest, textStatus, errorThrown) {
			 console.log(XMLHttpRequest);
			 console.log(textStatus);
			 console.log(errorThrown);
			 hideloading();
		}
	});
}
//查询盘点计划数据
function getData(){
	showloading();
	var sendData = {};
	sendData.startDate = $('#startDate').val();
	sendData.endDate = $('#endDate').val()
	sendData.shopcode=$('#shopcode').val();
   	sendData.flag=$('#flagSelect').val();
	$.ajax({
		url: "getPandianPlan"
		,type:'post'
//		,async: false
		,data:JSON.stringify(sendData)
		,contentType: "application/json; charset=utf-8"
		,dataType:'json'
		,success: function(res){
			try {
				$('#tableListDiv div[tableDiv]').remove();
				if(res.success){
					var resdata = res.data;
					var trStr='';
					for (var i = 0; i < resdata.length; i++) {
						if(resdata[i].flag==0){
							resdata[i].flagname="未开始";
						}else{
							resdata[i].flagname="已开始"
						}
						trStr += '<tr class="thin-border-bottom">'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+resdata[i].shopcode+'</span></td>'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;" id="'+resdata[i].shopcode+'-'+i+'">'+resdata[i].shopname+'</td>'//id="'+el+obj+i+'"
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+resdata[i].flagname+'</td>'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+resdata[i].pandianDate+'</td>'
							+'</tr>'
					}
					$('#tableListDiv').append('<div tableDiv>'
									+'	<div class="widget-box">'
									+'	<div class="widget-body">'
									+'		<div class="widget-main">'
									+'			<div class="clearfix">'
									+'				<div class="widget-main no-padding">'
									+'					<table class="table tabl	e-bordered table-striped">'
									+'						<thead class="thin-border-bottom">'
									+'							<tr>'
									+'								<th style="width: 10%;;padding-left: 0px;padding-right: 0px;">店号</th>'
									+'								<th style="width: 45%;;padding-left: 0px;padding-right: 0px;">店名</th>'
									+'								<th style="width: 20%">状态</th>'
									+'								<th>盘点日期</th>'
									+'							</tr>'
									+'						</thead>'
									+'						<tbody>'
									+							trStr
									+'						</tbody>'
									+'					</table>'
									+'				</div>'
									+'			</div>'
									+'		</div>'
									+'	</div>'
									+'</div>'
									+'</div>');
				}else{
					layer.alert(res.message);
				}
			} catch (e) {
				console.log(e);
			} finally{
				hideloading();
			}
			
		}
		,error:function(XMLHttpRequest, textStatus, errorThrown) {
			 console.log(XMLHttpRequest);
			 console.log(textStatus);
			 console.log(errorThrown);
			 hideloading();
		}
	});
}

function setTableList(){
	
										


}
